package com.miracle.common.util;

import android.content.Context;
import android.content.res.ColorStateList;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.widget.RadioButton;
import android.widget.RadioGroup;


import com.miracle.common.R;

import java.util.ArrayList;
import java.util.List;

/**
 * ExamineRadioButtonUtil.java
 * (c)2018 AIR Times Inc. All rights reserved.
 * RadioButton常用方法
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/6/13 16:11
 */
public class RadioButtonUtil {

    public static class RadioBean {
        /**
         * 标题
         * id
         * 是否选中
         */
        private String text;
        private String id;
        private boolean isChecked;

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public boolean isChecked() {
            return isChecked;
        }

        public void setChecked(boolean checked) {
            isChecked = checked;
        }
    }

    /**
     * 根据RadioButton ID 获取当前RadioButton的TAG
     * @param list
     * @param checkId
     * @return
     */
    public static int getTagByRadioGroup(List<RadioButton> list, int checkId) {
        if (list == null) {
            return 0;
        }
        RadioButton radioButton;
        for (int i = 0; i < list.size(); i++) {
            radioButton = list.get(i);
            if (radioButton.getId() == checkId) {
                return Integer.parseInt((String) radioButton.getTag());
            }
        }
        return 0;
    }

    //TODO 下一步需要将RadioGroup也直接创建，每4个需要换行等功能

    /**
     * 将RadioButton添加到RadioGroup上
     * 同时设置第一个为选中
     * @param mRadioList
     * @param radioGroup
     */
    public static ArrayList<RadioButton> addButtonToRadioGroup(Context context,
                                                               ArrayList<RadioBean> mRadioList,
                                                               RadioGroup radioGroup) {
        //获取屏幕宽度
        int width = ScreenUtils.getScreenWidth(context);
        //获取dp
        int dpWidth = ScreenUtils.px2dip(context, width);
        //获取padding，由于一行目前只显示4个
        int padding = (dpWidth - 72 * 4 - 32) / 3;

        ArrayList<RadioButton> buttonList = new ArrayList<>();
        if (mRadioList == null) {
            return null;
        }

        for (int i = 0; i < mRadioList.size(); i++) {
            RadioBean radioBean = mRadioList.get(i);
            RadioButton button = getRadioButton(context, radioBean.getText(), radioBean
                    .isChecked, radioBean.getId());
            buttonList.add(button);
            radioGroup.addView(button, getRadioButtonParams(context, (i == 0) ? false : true,
                    (padding > 0) ? padding : 0));
        }

        return buttonList;
    }

    /**
     * 获取设置style的RadioButton
     * @param text button字
     * @return RadioButton
     */
    public static RadioButton getRadioButton(Context context, String text, boolean isChecked, String
            tag) {
        RadioButton button = new RadioButton(context);
        button.setBackgroundResource(R.drawable.selector_button2_c401_c100);
        button.setButtonDrawable(null);
        int[][] states = new int[][]{new int[]{-android.R.attr.state_checked},
                new int[]{android.R.attr.state_checked}
        };
        int[] colors = new int[]{ContextCompat.getColor(context, R.color.c124), ContextCompat
                .getColor(context, R.color.c100)};
        ColorStateList colorStateList = new ColorStateList(states, colors);
        button.setTextColor(colorStateList);
        button.setGravity(Gravity.CENTER);
        button.setTextSize(12);
        button.setText(text);
        button.setChecked(isChecked);
        button.setTag(tag);
        return button;
    }

    /**
     * RadioButton params
     * @param setMargin
     * @return
     */
    public static RadioGroup.LayoutParams getRadioButtonParams(Context context, boolean
            setMargin, int margin) {
        RadioGroup.LayoutParams params = new RadioGroup.
                LayoutParams(ScreenUtils.dpToPx(context, 72), ScreenUtils.dpToPx
                (context, 28));
        if (setMargin) {
            params.setMargins(ScreenUtils.dpToPx(context, margin), 0, 0, 0);
        }

        return params;
    }

    /**
     * 设置选中状态ByTag
     * @param buttonList
     * @param tag
     */
    public static void setRadioButtonCheckedByTag(ArrayList<RadioButton> buttonList, String tag) {
        String tempTag;
        RadioButton button;
        for (int i = 0; i < buttonList.size(); i++) {
            button = buttonList.get(i);
            tempTag = (String) button.getTag();
            if (tag.equals(tempTag)) {
                button.setChecked(true);
                break;
            }
        }
    }

}
